package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.ObjectId;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.controller.param.ExportTicketParam;
import com.bestcem.xm.ticket.controller.v2.param.WeChatTicketSearchParam;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.dao.TicketDao;
import com.bestcem.xm.ticket.dao.TicketRecordDao;
import com.bestcem.xm.ticket.dao.converter.TicketConverter;
import com.bestcem.xm.ticket.dao.dto.TicketInfo;
import com.bestcem.xm.ticket.dao.mapper.TicketDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.ExportTicketParamDO;
import com.bestcem.xm.ticket.entity.mongo.Ticket;
import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.entity.mysql.TicketDO;
import com.bestcem.xm.ticket.enums.TicketStatusEnum;
import com.bestcem.xm.ticket.enums.TicketTypeEnum;
import com.bestcem.xm.ticket.enums.wechat.TicketSearchScopeEnum;
import com.bestcem.xm.ticket.grpc.client.TicketBaseUserGrpcService;
import com.bestcem.xm.ticket.grpc.client.TicketUserGrpcService;
import com.bestcem.xm.ticket.grpc.client.dto.user.GroupNodeDTO;
import com.bestcem.xm.ticket.service.dto.DatetimeFilter;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import com.bestcem.xm.ticket.service.dto.RegionFilter;
import com.bestcem.xm.ticket.service.dto.dashboard.DateTypeCountDTO;
import com.bestcem.xm.ticket.service.dto.dashboard.StatusCountDTO;
import com.bestcem.xm.ticket.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/24 11:39
 */
@Slf4j
@Repository
public class TicketDaoImpl implements TicketDao {

    /**
     * 已处理工单
     */
    private static final String SCOPE_DEAL = "deal";
    /**
     * 未处理工单
     */
    private static final String SCOPE_UNDEAL = "undeal";
    /**
     * 我创建的工单
     */
    private static final String MINE = "mine";
    /**
     * 前端传输的字符串true
     */
    private static final String TRUE = "true";
    /**
     * 前端传输的字符串false
     */
    private static final String FALSE = "false";
    /**
     * 查询条件对应的字段map
     */
    private static final Map<Integer, SFunction<TicketDO, ?>> DATA_TYPE_MAP = new HashMap<>();

    static {
        // 工单生成日期
        DATA_TYPE_MAP.put(0, TicketDO::getCreateTime);
        // 跟进截止日期
        DATA_TYPE_MAP.put(1, TicketDO::getFollowDeadline);
        // 审核截止日期
        DATA_TYPE_MAP.put(2, TicketDO::getAuditDeadline);
        // 工单关闭日期
        DATA_TYPE_MAP.put(5, TicketDO::getCloseTime);
        // 工单处理完成日期
        DATA_TYPE_MAP.put(6, TicketDO::getTicketFinishTime);
        // 工单处理失败日期
        DATA_TYPE_MAP.put(7, TicketDO::getTicketFailedTime);
    }

    @Resource
    private TicketBaseUserGrpcService ticketBaseUserGrpcService;
    @Resource
    private TicketUserGrpcService ticketUserGrpcService;
    @Resource
    private CommonUtils commonUtils;
    @Resource
    private TicketDaoMapper ticketDaoMapper;
    @Resource
    private TicketRecordDao ticketRecordDao;
    @Resource
    private TicketConfigDao ticketConfigDao;

    @Override
    public Ticket findById(String id) {
        TicketDO ticketDO = ticketDaoMapper.findByTicketId(id);

        return TicketConverter.doToDto(ticketDO);
    }

    /**
     * 根据configId查询 Ticket
     *
     * @param configId Ticket
     * @return Ticket 信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/11 15:36
     */
    @Override
    public Ticket findByConfigId(String configId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }

    @Override
    public Ticket findDeletedById(String id) {
        TicketDO ticketDO = ticketDaoMapper.findByTicketId(id);

        if (Objects.nonNull(ticketDO) && TicketStatusEnum.DELETED.getIndex().equals(ticketDO.getStatus())) {
            return TicketConverter.doToDto(ticketDO);
        }
        return null;
    }

    @Override
    public Ticket saveTicket(Ticket entity) {
        TicketDO entityDo = TicketConverter.dtoToDo(entity);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (ObjectUtils.isEmpty(entityDo.getTicketId())) {
            entityDo.setTicketId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        ticketDaoMapper.insert(entityDo);

        return TicketConverter.doToDto(entityDo);
    }

    @Override
    public Ticket overrideUpdateTicket(Ticket entity) {
        TicketDO entityDo = TicketConverter.dtoToDo(entity);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        entityDo.setUpdateTime(DateUtil.getCommonDate());
        ticketDaoMapper.updateByTicketId(entityDo);

        return TicketConverter.doToDto(entityDo);
    }

    /**
     * 查询公司下所有绑定了工单的问卷项目id
     *
     * @param orgId 公司id
     * @return 对应公司下所有绑定了工单的问卷项目id
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/16 15:06
     */
    @Override
    public List<String> findOrgAllSurvey(String orgId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }

    @Override
    public List<Ticket> findBySurveyId(String surveyId) {
        List<TicketDO> ticketDOList = ticketDaoMapper.listBySurveyId(surveyId);

        return TicketConverter.doListToDtoList(ticketDOList);
    }

    @Override
    public long countBySurveyId(String surveyId) {
        return ticketDaoMapper.countBySurveyId(surveyId);
    }

    /**
     * 根据问卷id查询最新工单
     *
     * @param surveyId
     * @return Ticket
     * @version v1.0
     * @author gy.liu
     * @date 2021/10/28
     */
    @Override
    public Ticket findLastBySurveyId(String surveyId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }

    /**
     * 机构查询
     *
     * @param orgId
     * @return List<Ticket>
     * @version v1.0
     * @author gy.liu
     * @date 2021/10/22
     */
    @Override
    public List<String> findByOrgId(String orgId) {
        List<Long> resIds = ticketDaoMapper.listResIdByOrgId(orgId);
        if (CollectionUtil.isEmpty(resIds)) {
            return new ArrayList<>();
        } else {
            List<String> surveyIdList = resIds.stream().filter(Objects::nonNull).map(String::valueOf).collect(Collectors.toList());
            return surveyIdList;
        }
    }

    @Override
    public Ticket findNext(String id, String orgId, String userId) {
        TicketDO nextTicket = ticketDaoMapper.findByOrgIdAndCurFollowUserIdAndStatusAndTicketId(orgId,
                userId, TicketStatusEnum.FOLLOWING.getIndex(), id);
        if (Objects.isNull(nextTicket)) {
            nextTicket = ticketDaoMapper.findByOrgIdAndCurFollowUserIdAndStatus(orgId,
                    userId, TicketStatusEnum.FOLLOWING.getIndex());
        }
        return TicketConverter.doToDto(nextTicket);
    }

    @Override
    public List<TicketInfo> lookUpTicketRecord(String id) {
        TicketDO ticketDO = ticketDaoMapper.findByTicketId(id);

        List<TicketInfo> list = new ArrayList<>();
        if (Objects.nonNull(ticketDO) && !TicketStatusEnum.DELETED.getIndex().equals(ticketDO.getStatus())) {
            Ticket ticket = TicketConverter.doToDto(ticketDO);
            List<TicketRecord> ticketRecordList = ticketRecordDao.listByTicketId(id);
            TicketInfo ticketInfo = new TicketInfo();
            BeanUtils.copyProperties(ticket, ticketInfo);
            ticketInfo.setTicketRecords(ticketRecordList);
            list.add(ticketInfo);
        }
        return list;
    }

    @Override
    public boolean updateStatus(String id, TicketStatusEnum status) {
        TicketDO entityDo = new TicketDO();
        entityDo.setTicketId(id);
        entityDo.setStatus(status.getIndex());
        if (TicketStatusEnum.FAILED.getIndex().equals(status.getIndex())) {
            entityDo.setTicketFailedTime(new Date());
        }
        if (TicketStatusEnum.FINISH.getIndex().equals(status.getIndex())) {
            entityDo.setTicketFinishTime(new Date());
        }
        entityDo.setUpdateTime(DateUtil.getCommonDate());

        ticketDaoMapper.updateByTicketId(entityDo);

        return true;
    }

    @Override
    public PageDTO<Ticket> newSearch(ExportTicketParam param) {
        ExportTicketParamDO paramDO = new ExportTicketParamDO();
        BeanUtils.copyProperties(param, paramDO);

        PageDTO<Ticket> pageDTO = new PageDTO<>();

        paramDO.setVerifiedDateType(false);
        if (Objects.nonNull(param.getDateType())) {
            if (DATA_TYPE_MAP.containsKey(param.getDateType())) {
                paramDO.setVerifiedDateType(true);
                if (StringUtils.isNotEmpty(param.getStartTime())) {
                    Date startDate = DateUtil.getDate(param.getStartTime());
                    paramDO.setStartDate(startDate);
                }
                if (StringUtils.isNotEmpty(param.getEndTime())) {
                    Date endDate = DateUtil.getDate(param.getEndTime() + " 23:59:59");
                    paramDO.setEndDate(endDate);
                }
            }
        }

        // 工单范围过滤
        if (SCOPE_UNDEAL.equals(param.getScope())) {
            List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
            paramDO.setRoleIdList(roleIds);
        } else if (TicketTypeEnum.ALL.getName().equals(param.getScope())) {
            List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
            paramDO.setRegionIdList(commonUtils.getUserAllRegions(param.getUserId()));
            paramDO.setCreatedId(param.getUserId());
            paramDO.setRoleIdList(roleIds);
        }

        paramDO.setVerifiedStatus(false);
        if (Objects.nonNull(TicketStatusEnum.getByIndex(param.getStatus()))) {
            paramDO.setVerifiedStatus(true);
        }

        // 查询总数
        Long total = ticketDaoMapper.countByExportTicketParam(paramDO);
        pageDTO.setTotal(total);

        // 分页
        if (Objects.nonNull(param.getPage()) && Objects.nonNull(param.getRowsPerPage())) {
            Integer page = param.getPage();
            int size = param.getRowsPerPage();
            int start = (page - 1) * size;
            paramDO.setStart(start);
            paramDO.setSize(size);
        }

        List<TicketDO> ticketDOList = ticketDaoMapper.listByExportTicketParam(paramDO);
        List<Ticket> ticketList = TicketConverter.doListToDtoList(ticketDOList);
        pageDTO.setData(ticketList);
        return pageDTO;
    }

    @Override
    public List<StatusCountDTO> aggregateStatusCount(RegionFilter regionFilter, DatetimeFilter datetimeFilter) {
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(regionFilter.getUserId());
        regionFilter.setUserCurrentRegions(userCurrentRegions);
        return ticketDaoMapper.countByStatus(regionFilter, datetimeFilter);
    }

    @Override
    public Long aggregateTicketFollowOverdueCount(RegionFilter regionFilter, DatetimeFilter datetimeFilter) {
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(regionFilter.getUserId());
        regionFilter.setUserCurrentRegions(userCurrentRegions);
        return ticketDaoMapper.countHasFollowOverdue(regionFilter, datetimeFilter);
    }

    @Override
    public Long aggregateTicketAuditOverdueCount(RegionFilter regionFilter, DatetimeFilter datetimeFilter) {
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(regionFilter.getUserId());
        regionFilter.setUserCurrentRegions(userCurrentRegions);
        return ticketDaoMapper.countHasAuditOverdue(regionFilter, datetimeFilter);
    }

    @Override
    public List<DateTypeCountDTO> aggregateTicketCount(RegionFilter regionFilter, LocalDateTime startTime, LocalDateTime endTime) {
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(regionFilter.getUserId());
        regionFilter.setUserCurrentRegions(userCurrentRegions);
        return ticketDaoMapper.countByDate(regionFilter, startTime, endTime);
    }

    @Override
    public List<Ticket> findBySeq(Integer seq) {
        List<TicketDO> ticketDOList = ticketDaoMapper.listByResId(seq);

        return TicketConverter.doListToDtoList(ticketDOList);
    }

    @Override
    public void updateTicketMemberId(String id, String memberId) {
        TicketDO entityDo = new TicketDO();
        entityDo.setTicketId(id);
        entityDo.setMemberId(memberId);

        entityDo.setUpdateTime(DateUtil.getCommonDate());
        ticketDaoMapper.updateByTicketId(entityDo);
    }

    @Override
    public List<Ticket> findTicketsByConfigId(String configId) {
        List<TicketDO> ticketDOS = ticketDaoMapper.listUsableByConfigId(configId);

        return TicketConverter.doListToDtoList(ticketDOS);

    }

    @Override
    public void updateTicketRecordId(Ticket ticket) {

        List<String> ticketRecordIds = ticket.getTicketRecordIds();

        if (CollUtil.isNotEmpty(ticketRecordIds)) {
            String ticketArrayJson = JSONArray.toJSONString(ticketRecordIds);
            ticketDaoMapper.updateTicketRecordId(ticket.getId(), ticketArrayJson);
        }


    }

    /**
     * 行业sample保存工单
     *
     * @param entity
     * @return Ticket
     * @version v1.0
     * @author gy.liu
     * @date 2021/10/19
     */
    @Override
    public Ticket saveTicketCopy(Ticket entity) {
        return saveTicket(entity);
    }

    /**
     * 根据导出参数获取导出的工单数据
     * !!!注意, 改了这个方法, 要联动修改仪表盘的查询方法, 要保证导出的 工单/预警 数量与仪表盘的数量显示一致
     * {@link TicketDao#aggregateStatusCount}
     *
     * @param param 传入的参数
     * @return 根据入参返回的list
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/11/17 15:23
     */
    @Override
    public List<Ticket> listExportTicket(ExportTicketParam param) {
        ExportTicketParamDO paramDO = new ExportTicketParamDO();
        BeanUtils.copyProperties(param, paramDO);

        paramDO.setVerifiedDateType(false);
        if (Objects.nonNull(param.getDateType())) {
            if (DATA_TYPE_MAP.containsKey(param.getDateType())) {
                paramDO.setVerifiedDateType(true);
                if (StringUtils.isNotEmpty(param.getStartTime())) {
                    Date startDate = DateUtil.getDate(param.getStartTime());
                    paramDO.setStartDate(startDate);
                }
                if (StringUtils.isNotEmpty(param.getEndTime())) {
                    Date endDate = DateUtil.getDate(param.getEndTime() + " 23:59:59");
                    paramDO.setEndDate(endDate);
                }
            }
        }

        // 工单范围过滤
        if (SCOPE_UNDEAL.equals(param.getScope())) {
            List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
            paramDO.setRoleIdList(roleIds);
        } else if (TicketTypeEnum.ALL.getName().equals(param.getScope())) {
            List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
            paramDO.setRegionIdList(commonUtils.getUserAllRegions(param.getUserId()));
            paramDO.setCreatedId(param.getUserId());
            paramDO.setRoleIdList(roleIds);
        }

        paramDO.setVerifiedStatus(false);
        if (Objects.nonNull(TicketStatusEnum.getByIndex(param.getStatus()))) {
            paramDO.setVerifiedStatus(true);
        }

        List<TicketDO> ticketDOList = ticketDaoMapper.listByExportTicketParam(paramDO);
        return TicketConverter.doListToDtoList(ticketDOList);
    }

    /**
     * 根据导出参数获取分页查询的工单数据
     * !!!注意, 改了这个方法, 要联动修改仪表盘的查询方法, 要保证分页查询的 工单/预警 total 与仪表盘的数量显示一致
     * {@link TicketDao#aggregateStatusCount}
     *
     * @param param 传入的参数
     * @return 根据入参返回的list
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/11/17 15:23
     */
    @Override
    public PageDTO<Ticket> pageTicket(ExportTicketParam param) {
        ExportTicketParamDO paramDO = new ExportTicketParamDO();
        BeanUtils.copyProperties(param, paramDO);

        PageDTO<Ticket> pageDTO = new PageDTO<>();

        paramDO.setVerifiedDateType(false);
        if (Objects.nonNull(param.getDateType())) {
            if (DATA_TYPE_MAP.containsKey(param.getDateType())) {
                paramDO.setVerifiedDateType(true);
                if (StringUtils.isNotEmpty(param.getStartTime())) {
                    Date startDate = DateUtil.getDate(param.getStartTime());
                    paramDO.setStartDate(startDate);
                }
                if (StringUtils.isNotEmpty(param.getEndTime())) {
                    Date endDate = DateUtil.getDate(param.getEndTime() + " 23:59:59");
                    paramDO.setEndDate(endDate);
                }
            }
        }

        // 工单范围过滤
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(param.getUserId());
        if (SCOPE_UNDEAL.equals(param.getScope())) {
            List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
            paramDO.setRoleIdList(roleIds);
            paramDO.setUserCurrentRegions(userCurrentRegions);
        } else if (TicketTypeEnum.ALL.getName().equals(param.getScope())) {
            if (!Integer.valueOf(1).equals(param.getSuperAdmin())) {
                List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
                paramDO.setRegionIdList(commonUtils.getUserAllRegions(param.getUserId()));
                paramDO.setUserCurrentRegions(userCurrentRegions);
                paramDO.setCreatedId(param.getUserId());
                paramDO.setRoleIdList(roleIds);
            }
        }

        paramDO.setVerifiedStatus(false);
        if (Objects.nonNull(TicketStatusEnum.getByIndex(param.getStatus()))) {
            paramDO.setVerifiedStatus(true);
        }

        // 查询总数
        Long total = ticketDaoMapper.countByExportTicketParam(paramDO);
        pageDTO.setTotal(total);

        // 分页
        if (Objects.nonNull(param.getPage()) && Objects.nonNull(param.getRowsPerPage())) {
            Integer page = param.getPage();
            int size = param.getRowsPerPage();
            int start = (page - 1) * size;
            paramDO.setStart(start);
            paramDO.setSize(size);
        }

        List<TicketDO> ticketDOList = ticketDaoMapper.listByExportTicketParam(paramDO);
        List<Ticket> ticketList = TicketConverter.doListToDtoList(ticketDOList);
        pageDTO.setData(ticketList);
        return pageDTO;
    }

    /**
     * 企业微信小程序定制版-根据参数获取分页搜索工单
     *
     * @param param 传入的参数
     * @return 根据入参返回的list
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/12/17 16:55
     */
    @Override
    public PageDTO<Ticket> wechatPageTicket(WeChatTicketSearchParam param) {
        ExportTicketParamDO paramDO = new ExportTicketParamDO();
        PageDTO<Ticket> pageDTO = new PageDTO<>();


        /* 工单范围框过滤 */
        List<String> roleIds = commonUtils.getUserAllRoleId(param.getUserId());
        List<String> userCurrentRegions = commonUtils.getUserCurrentGroupId(param.getUserId());
        if (TicketSearchScopeEnum.PENDING.getIndex().equals(param.getScope())) {
            paramDO.setRoleIdList(roleIds);
            paramDO.setUserCurrentRegions(userCurrentRegions);
            paramDO.setCreatedId(param.getUserId());
        }
        if (TicketSearchScopeEnum.ALL.getIndex().equals(param.getScope())) {
            if (!param.isSuper()) {
                paramDO.setRegionIdList(commonUtils.getUserAllRegions(param.getUserId()));
                paramDO.setRoleIdList(roleIds);
                paramDO.setUserCurrentRegions(userCurrentRegions);
                paramDO.setCreatedId(param.getUserId());
            }
        }

        /* 下拉/搜索 框过滤 */
        // 只查询当前租户的数据
        paramDO.setOrgId(param.getOrgId());
        // 搜索框过滤 (编号 | 工单名称 | 规则名称)
        if (Objects.nonNull(param.getSearch()) && !ObjectUtils.isEmpty(param.getSearch().trim())) {
            paramDO.setWechatWork(true);
            // 查询关键词对应的工单规则id
            List<String> ticketConfigIds = ticketConfigDao.searchTicketConfigId(param.getOrgId(), param.getSearch());
            paramDO.setSearch(param.getSearch());
            paramDO.setTicketConfigIds(ticketConfigIds);
        }

        // 工单类型过滤
        paramDO.setTtype(param.getType());
        // 工单状态过滤
        paramDO.setVerifiedStatus(false);
        if (Objects.nonNull(TicketStatusEnum.getByIndex(param.getStatus()))) {
            paramDO.setVerifiedStatus(true);
            paramDO.setStatus(param.getStatus());
        }
        // 所属组织过滤
        if (ObjectId.isValid(param.getGroupId())) {
            // 获取传入组织的下属所有组织
            List<String> subGroupIdList = new ArrayList<>();
            List<GroupNodeDTO> groupNodeDTOList =
                    ticketUserGrpcService.listSubGroupsByGroupIdsAndLevelId(Collections.singletonList(param.getGroupId()), null);
            if (!CollectionUtils.isEmpty(groupNodeDTOList)) {
                for (GroupNodeDTO groupNodeDTO : groupNodeDTOList) {
                    if (Objects.nonNull(groupNodeDTO)) {
                        subGroupIdList.add(groupNodeDTO.getId());
                    }
                }
            }
            paramDO.setSubGroupIds(subGroupIdList);
        }

        /* 时间范围过滤 */
        paramDO.setVerifiedDateType(false);
        if (Objects.nonNull(param.getStartTime()) || Objects.nonNull(param.getEndTime())) {
            paramDO.setVerifiedDateType(true);
            paramDO.setDateType(0);
            LocalDateTime startTime = null, endTime = null;
            Date startDate = null, endDate = null;
            if (StringUtils.isNotEmpty(param.getStartTime())) {
                startTime = DateUtil.getLocalDateTime(param.getStartTime());
                startDate = DateUtil.getCommonDate(startTime);
            }
            if (StringUtils.isNotEmpty(param.getEndTime())) {
                endTime = DateUtil.getLocalDateTime(param.getEndTime() + " 23:59:59");
                endDate = DateUtil.getCommonDate(endTime);
            }
            if (Objects.nonNull(startDate)) {
                paramDO.setStartDate(startDate);
            }
            if (Objects.nonNull(endDate)) {
                paramDO.setEndDate(endDate);
            }
        }

        /* 排序; 按照生成时间降序 */
        paramDO.setSortBy("created_dt");
        paramDO.setDescending("true");

        /* 查询总数 */
        Long total = ticketDaoMapper.countByExportTicketParam(paramDO);
        pageDTO.setTotal(total);

        /* 分页 */
        int page = Optional.ofNullable(param.getPage()).orElse(0);
        int size = Optional.ofNullable(param.getSize()).orElse(10);
        int skip = page * size;
        paramDO.setStart(skip);
        paramDO.setSize(size);

        List<TicketDO> ticketDOList = ticketDaoMapper.listByExportTicketParam(paramDO);
        List<Ticket> ticketList = TicketConverter.doListToDtoList(ticketDOList);
        pageDTO.setData(ticketList);
        return pageDTO;
    }

}
